package com.xtpt.disaster.controller;

import com.xtpt.disaster.bean.DbnodeInfo;
import com.xtpt.disaster.bean.JobDetail;
import com.xtpt.disaster.bean.NodeInfo;
import com.xtpt.disaster.common.PortalResult;
import com.xtpt.disaster.service.DBNodeInfoService;
import com.xtpt.disaster.service.JobDetailService;
import com.xtpt.disaster.service.NodeInfoManageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.*;

@CrossOrigin
@RestController
@Api(tags = "作业信息管理接口")
@RequestMapping("/manage/jobs")
public class JobDetailController {

    @Autowired
    private JobDetailService jobDetailService;

    @Autowired
    NodeInfoManageService nodeInfoManageService;

    @Autowired
    DBNodeInfoService dbNodeInfoService;

    @ApiOperation(value = "根据页面查询")
    @GetMapping("/page/select")
    public PortalResult slectByPage(@RequestParam(defaultValue = "1")Integer pageNo,
                                    @RequestParam(defaultValue = "10")Integer pageSize,
                                    @RequestParam(required = false) JobDetail jobDetail){
        return jobDetailService.getJobsPage(jobDetail, pageNo, pageSize);
    }

    @ApiOperation(value = "根据ID查询作业信息")
    @PostMapping ("/delete")
    public PortalResult deleteJob(@RequestBody Long jobId){
        return  jobDetailService.delete(jobId);
    }

    @ApiOperation(value = "添加作业信息")
    @PostMapping("/add")
    public PortalResult add(@RequestBody JobDetail jobDetail){
        return  jobDetailService.insert(jobDetail);
    }

    @ApiOperation(value = "根据id数组批量删除作业信息")
    @PostMapping ("/batch/delete")
    public PortalResult deleteBatch(@RequestBody long[] ids) {
        return jobDetailService.deleteBatch(ids);
    }

    @ApiOperation(value = "获取所有作业信息")
    @GetMapping("/getall")
    public PortalResult getAll() {
        List<JobDetail> list = jobDetailService.getAll();
        return PortalResult.ok(list);
    }

    @ApiOperation(value = "根据id获取作业信息")
    @GetMapping("/getbyid")
    public PortalResult getById(@RequestParam Long id) {
        return PortalResult.ok(jobDetailService.getById(id));
    }

    @ApiOperation(value = "根据Jobdetail任意条件查询作业信息")
    @PostMapping ("/getbycon")
    public PortalResult getByCondition(@RequestBody JobDetail jobDetail) {
        List<JobDetail> list = jobDetailService.getJobs(jobDetail);
        for (JobDetail job:list) {
            Long nodeId = job.getNodeId();
            if (nodeId != null) {
                NodeInfo nodeInfo = nodeInfoManageService.selectById(nodeId);
                if (nodeInfo != null) {
                    String nodeIp = nodeInfo.getNodeIp();
                    job.setNodeIp(nodeIp);
                }
            } else {
                DbnodeInfo dbnodeInfo = dbNodeInfoService.selectByPrimaryKey(job.getDbId());
                if (dbnodeInfo != null) {
                    String nodeIp = dbnodeInfo.getNodeIp();
                    job.setNodeIp(nodeIp);
                }
            }
        }
        return PortalResult.ok(list);
    }

    @ApiOperation(value = "根据Jobdetail任意条件查询作业记录条数")
    @PostMapping ("/getbycon/count")
    public PortalResult getCountByCondition(@RequestBody JobDetail jobDetail) {
        List<JobDetail> list = jobDetailService.getJobs(jobDetail);
        Integer count = list.size();
        return PortalResult.ok(count);
    }

    @ApiOperation(value = "查询一周7天内的记录条数")
    @PostMapping ("/getbycon/count/week")
    public PortalResult getCountOfOneWeek(@RequestBody JobDetail jobDetail) {
        List<Map<String, Object>> result = jobDetailService.selectCountByDay(jobDetail);
        return PortalResult.ok(result);
    }


    @ApiOperation(value = "根据jobId删除作业信息")
    @DeleteMapping("/delete")
    public PortalResult deleteById(@RequestParam Long id) {
        return jobDetailService.delete(id);
    }
}
